using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Material.Entity;
using Material.Object;
using System.Configuration;
using Material.App;

namespace App.Material
{
    public partial class FrmSatuan : Form
    {
        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();
        private TblSatuan tblSatuan;
        private TblSatuanEntity tblSatuanEntity;
        private string stateInput = string.Empty;
        private int IdSatuan = 0;

        public FrmSatuan()
        {
            InitializeComponent();
            ClearTextbox();
            EnabledTextboxs(false, false);
            EnabledButtons(true, false, false, true);
            BindGridData();

            dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Green;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
           
        }

        /// <summary>
        /// Kondisi untuk Disable dan Enable Kontrol [Textbox dan Button]
        /// </summary>
        /// <param name="state">boolean True dan False</param>
        private void EnabledTextboxs(bool bltxtSatuan,bool bltxtKeterangan)
        {
            txtSatuan.Enabled = bltxtSatuan;
            //txtKeterangan.Enabled = bltxtKeterangan;
            
        }

        /// <summary>
        /// Kondisi Untuk Disable dan Enable Button
        /// </summary>
        /// <param name="blbtnNew"></param>
        /// <param name="blbtnCancel"></param>
        /// <param name="blbtnSave"></param>
        /// <param name="blbtnPrint"></param>
        private void EnabledButtons(bool blbtnNew, bool blbtnCancel, bool blbtnSave, bool blbtnPrint)
        {
            btnNew.Enabled = blbtnNew;
            btnCancel.Enabled = blbtnCancel;
            btnSave.Enabled = blbtnSave;
            btnPrint.Enabled = blbtnPrint;
            
        }

        /// <summary>
        /// Clear Semua TextBox
        /// </summary>
        private void ClearTextbox()
        {
            //txtKeterangan.Text = string.Empty;
            txtSatuan.Text = string.Empty;
        }

        private void FrmSatuan_Load(object sender, EventArgs e)
        {

        }

        private void btnFind_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtSatuan.Text.Trim().Length > 0)
            {
                tblSatuan = new TblSatuan();
                tblSatuanEntity = new TblSatuanEntity(connStr);
                int i = 0;

                tblSatuan.Satuan = txtSatuan.Text;
                tblSatuan.Keterangan = "";
                if (stateInput == "Add")
                {
                    tblSatuan.CreatedDate = DateTime.Now;
                    tblSatuan.CreatedBy = FrmLogin.UserID;
                    //tblSatuan.ModifiedDate = DateTime.Now;
                    //tblSatuan.ModifiedBy = FrmLogin.UserID;
                    i = tblSatuanEntity.Insert(tblSatuan);
                }
                else
                {
                    tblSatuan.Id = IdSatuan;
                    tblSatuan.ModifiedBy = FrmLogin.UserID;
                    tblSatuan.ModifiedDate = DateTime.Now;
                    i = tblSatuanEntity.Update(tblSatuan);
                }
                if (i != -1)
                {
                    ClearTextbox();
                    EnabledTextboxs(false, false);
                    EnabledButtons(true, false, false, true);
                    BindGridData();
                    MessageBox.Show("Simpan data berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                    MessageBox.Show("Simpan data tidak berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);


            }
            else
            {
                MessageBox.Show("Kolom Satuan Harus Diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSatuan.Focus();
            }
            
        }

        private void BindGridData()
        {
            tblSatuanEntity = new TblSatuanEntity(connStr);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = tblSatuanEntity.GetDataAll();
           
          
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            EnabledTextboxs(true, true);
            EnabledButtons(false, true, true, false);
            txtSatuan.Focus();
            stateInput = "Add";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            EnabledTextboxs(false, false);
            EnabledButtons(true, false, false, true);
            ClearTextbox();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {

                switch (e.ColumnIndex)
                {
                    case 3:
                        ClearTextbox();
                        EnabledTextboxs(true, true);
                        EnabledButtons(false, true, true, false);
                        tblSatuanEntity = new TblSatuanEntity(connStr);
                        tblSatuan = new TblSatuan();
                        tblSatuan.Id = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                        tblSatuan = tblSatuanEntity.GetDataByID(tblSatuan);
                        //txtKeterangan.Text = tblSatuan.Keterangan;
                        txtSatuan.Text = tblSatuan.Satuan;
                        IdSatuan = tblSatuan.Id;
                        stateInput = "Edit";
                        break;

                    case 4:
                        DialogResult dr = MessageBox.Show("Data akan dihapus?", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dr == DialogResult.Yes)
                        {
                            tblSatuanEntity = new TblSatuanEntity(connStr);
                            tblSatuan = new TblSatuan();
                            tblSatuan.Id =Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                            int i = tblSatuanEntity.Delete(tblSatuan);
                            BindGridData();
                        }
                        break;
                }
                         
                }
            }

        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //// Check which column is selected, otherwise set NewColumn to null.
            //DataGridViewColumn newColumn =
            //    dataGridView1.SelectedColumns.Count > 0 ?
            //    dataGridView1.SelectedColumns[0] : null;

            //DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
            //ListSortDirection direction;

            //// If oldColumn is null, then the DataGridView is not currently sorted.
            //if (oldColumn != null)
            //{
            //    // Sort the same column again, reversing the SortOrder.
            //    if (oldColumn == newColumn &&
            //        dataGridView1.SortOrder == SortOrder.Ascending)
            //    {
            //        direction = ListSortDirection.Descending;
            //    }
            //    else
            //    {
            //        // Sort a new column and remove the old SortGlyph.
            //        direction = ListSortDirection.Ascending;
            //        oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
            //    }
            //}
            //else
            //{
            //    direction = ListSortDirection.Ascending;
            //}

            //dataGridView1.Sort(newColumn, direction);

            //newColumn.HeaderCell.SortGlyphDirection =
            //    direction == ListSortDirection.Ascending ?
            //    SortOrder.Ascending : SortOrder.Descending;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            FrmRptSatuan frm = new FrmRptSatuan();
            frm.ShowDialog();
        }

        private void FrmSatuan_ResizeBegin(object sender, EventArgs e)
        {
           
        }

        private void btnFind_Click_1(object sender, EventArgs e)
        {
            tblSatuan = new TblSatuan();
            tblSatuan.Satuan = txtFind.Text;

            tblSatuanEntity = new TblSatuanEntity(connStr);

            dataGridView1.DataSource = tblSatuanEntity.GetFindData(tblSatuan).Tables[0];
        }
        
    }
}